﻿using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WenSkin.Controls
{
    public class ToolStripRenderer : ToolStripProfessionalRenderer
    {
        private Color menuItemSelectedColor = Color.FromArgb(51, 51, 52);
        private Color menuItemBorderColor = Color.FromArgb(51, 51, 52);
        private Color DefaultTextColor = Color.White;

        public ToolStripRenderer() : base(new ToolStripBarColor())
        {
            
        }

        protected override void OnRenderImageMargin(ToolStripRenderEventArgs e)
        {
            //base.OnRenderImageMargin(e);
            //屏蔽掉左边图片竖条
        }
        //渲染边框 不绘制边框
        protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
        {
            //不调用基类的方法 屏蔽掉该方法 去掉边框
        }

        // 渲染背景 包括menustrip背景 toolstripDropDown背景
        protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e)
        {
            ToolStrip toolStrip = e.ToolStrip;
            Graphics g = e.Graphics.SetGDIHigh();
            switch (toolStrip)
            {
                case MenuStrip _:
                    //由menuStrip的Paint方法定义 这里不做操作
                    break;
                case ToolStripDropDown _:
                    {
                        Rectangle rect = new Rectangle(Point.Empty, toolStrip.Size);
                        g.FillRectangle(new SolidBrush(ColorTable.ToolStripDropDownBackground), rect);
                        break;
                    }
                default:
                    base.OnRenderToolStripBackground(e);
                    break;
            }
        }

        //重画文字
        protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
        {
            //文字颜色修改
            base.OnRenderItemText(e);
        }
        //重画单项颜色
        protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
        {
            if (e.Item.IsOnDropDown)
            {
                if (e.Item.Selected == true && e.Item.Enabled)
                {
                    DrawMenuDropDownItemHighlight(e);
                }
            }
            else
            {
                base.OnRenderMenuItemBackground(e);
            }
        }

        private void DrawMenuDropDownItemHighlight(ToolStripItemRenderEventArgs e)
        {
            Rectangle rect = new Rectangle(2, 0,
                (int)e.Graphics.VisibleClipBounds.Width - 4,
                (int)e.Graphics.VisibleClipBounds.Height - 1);

            using SolidBrush brush = new SolidBrush(menuItemSelectedColor);
            e.Graphics.FillRectangle(brush, rect);

            using Pen pen = new Pen(menuItemBorderColor);
            e.Graphics.DrawRectangle(pen, rect);
        }

        protected override void OnRenderDropDownButtonBackground(ToolStripItemRenderEventArgs e)
        {
            Graphics g = e.Graphics.SetGDIHigh();
            switch (e.Item)
            {
                case ToolStripDropDownButton stripDropDownButton:
                    if (stripDropDownButton.Selected)
                    {
                        Rectangle rect = new Rectangle(Point.Empty, stripDropDownButton.Size);
                        g.FillRectangle(new SolidBrush(ColorTable.MenuItemPressedGradientBegin), rect);
                    }
                    break;
                default:
                    base.OnRenderDropDownButtonBackground(e);
                    break;
            }

        }
        protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)
        {
            Graphics g = e.Graphics.SetGDIHigh();
            switch (e.Item)
            {
                case ToolStripButton button:
                    if (button.Selected)
                    {
                        Rectangle rect = new Rectangle(Point.Empty, button.Size);
                        g.SetClip(rect);
                        g.FillRectangle(new SolidBrush(ColorTable.MenuItemPressedGradientBegin), rect);
                    }
                    break;
                default:
                    base.OnRenderButtonBackground(e);
                    break;
            }
        }
    }
}